package com.firebird.app.reader.pdf.filehelper

import android.os.Environment
import java.io.File

object FileUtils {//获取跟目录//判断sd卡是否存在
    /**
     * 获取SD卡路径
     *
     * @return
     */
    val sDPath: String?
        get() {
            var sdDir: File? = null
            val sdCardExist = (Environment.getExternalStorageState()
                    == Environment.MEDIA_MOUNTED) //判断sd卡是否存在
            if (sdCardExist) {
                sdDir = Environment.getExternalStorageDirectory() //获取跟目录
            }
            try {
                return sdDir.toString()
            } catch (e: NullPointerException) {
            }
            return null
        }

    /**
     * 删除所有文件
     *
     * @param filePath
     * @return
     */
    fun deleteFile(filePath: String): Boolean {
        val file = File(filePath)
        if (file.exists()) { //如果该文件或者目录存在
            if (!file.isDirectory) { //                System.out.println("是文件");
                file.delete()
                //                System.out.println("文件删除成功1111111111");
            } else if (file.isDirectory) { //                System.out.println("是文件夹！！！！！");
                val fileList = file.list()
                for (i in fileList.indices) {
                    val delFile = File(filePath + "/" + fileList[i])
                    if (!delFile.isDirectory) {
                        println("path=" + delFile.path)
                        delFile.delete()
                        //                        System.out.println("删除文件成功");
                    } else if (delFile.isDirectory) {
                        deleteFile(filePath + "/" + fileList[i])
                    }
                }
                file.delete()
                //                System.out.println("文件夹删除成功2222222222222！");
            }
        } else if (!file.exists()) { //如果该文件或者目录不存在
//            System.out.println("该文件或目录不存在！");
        }
        return true
    }
}